<?php
define('DISABLEDEFENSE', true);
define('DISABLEXSSCHECK', true);

require '../../../../class/class_core.php';
C::app()->init();


require_once "EpayCore.php";

global $_G;

define('BOAN_PAYMENT_NAME', 'plugin/boan_payment');

if(empty($_G['cache']['plugin'])){
    
    loadcache('plugin');
    
}

$vars = $_G['cache']['plugin']['boan_payment'];

if(empty($vars['epay_pid']) || empty($vars['epay_key']) || $vars['eapy_url']){
   exit();
}

$config = array('pid' => $vars['epay_pid'],'key' => $vars['epay_key'],'apiurl' =>  $vars['epay_url']);


$epay = new EpayCore($config);

if(!$epay->verifyNotify()){
    exit();
}


$trade_order_id = $_GET['out_trade_no'];

if($_GET['trade_status'] == 'TRADE_SUCCESS'){
    
    $order = C::t('#boan_payment#boan_payment_order')->fetch($trade_order_id);
    
    $order_id = $order['order_id'];
    
    $lockid = 'pay_'.$order_id;
    
    if(!discuz_process::islocked($lockid)){
        
        if($order['status'] == 0){
            
            $pay_method = $_GET['type'];
            
            C::m('#boan_payment#payment')->updateOrder($order_id,['pay_method'=>$pay_method,'status'=>1,'pay_time' => time(),'trade_id'=>$_GET['trade_no']]);
            
            C::m('#boan_payment#payment')->log($trade_order_id,2,$pay_method,lang(BOAN_PAYMENT_NAME, 'order_info1'), 2,$_GET);
            
            C::m('#boan_payment#payment')->pluginNotify($order_id,'payNotify');
            
        }
        discuz_process::unlock($lockid);
    }
}

echo 'success';